﻿using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Waf.Applications;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ImageClipTool.Applications.ViewModels;
using ImageClipTool.Presentation.Views;
using ImageClipTool.Utils;

namespace ImageClipTool.Applications.Controllers
{
    [Export]
    internal class ImageClipController
    {
        private readonly ShellViewModel shellViewModel;
        private readonly ImageClipViewModel imageClipViewModel;
        private readonly DelegateCommand cutImageCommand;
        
        private ImageClipWindow imageClipWindow;
        private BitmapImage bi;
        public System.Drawing.Bitmap zoneBitmap; // 缓存区域截图。如果之后的其他操作需要使用裁剪后的图片，从这里取得。


        [ImportingConstructor]
        public ImageClipController(ShellViewModel shellViewModel, ImageClipViewModel imageClipViewModel)
        {
            this.shellViewModel = shellViewModel;
            this.imageClipViewModel = imageClipViewModel;
            this.cutImageCommand = new DelegateCommand(CutImageCommand);
        }


        public void Initialize()
        {
            imageClipWindow = imageClipViewModel.View as ImageClipWindow;
            imageClipViewModel.CutImageCommand = this.cutImageCommand;
            // 滑动条事件
            imageClipWindow.scaleSlider.ValueChanged += ScaleSlider_ValueChanged;
        }

        internal void Show(BitmapImage sourceBi)
        {
            byte[] imageInfo = SystemUtils.Instance.ConvertBitmapImageToByteArray(sourceBi);
            double dpiScaleX = sourceBi.DpiX / 96; // WPF默认使用96DPI
            double dpiScaleY = sourceBi.DpiY / 96;

            using (MemoryStream imageStream = new MemoryStream(imageInfo))
            {
                bi = new BitmapImage();
                bi.BeginInit();
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.DecodePixelWidth = (int)(sourceBi.PixelWidth * dpiScaleX);
                bi.DecodePixelHeight = (int)(sourceBi.PixelHeight * dpiScaleY);
                bi.StreamSource = imageStream;
                bi.EndInit();
            }
            imageClipWindow.originalImage.Source = bi;

            // 根据原图大小，调整Image控件的宽高
            imageClipWindow.originalImage.Width = bi.Width > imageClipViewModel.ORIGINAL_IMAGE_WIDTH ? bi.Width : imageClipViewModel.ORIGINAL_IMAGE_WIDTH;
            imageClipWindow.originalImage.Height = bi.Height > imageClipViewModel.ORIGINAL_IMAGE_HEIGHT ? bi.Height : imageClipViewModel.ORIGINAL_IMAGE_HEIGHT;
            imageClipWindow.scaleSlider.Value = 1;

            imageClipWindow.ShowDialog();
        }

        private void ScaleSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            double scale = e.NewValue;
            TransformGroup tg = imageClipWindow.originalImage.RenderTransform as TransformGroup;
            var tgnew = tg.CloneCurrentValue();
            if (tgnew != null)
            {
                ScaleTransform st = tgnew.Children[0] as ScaleTransform;
                imageClipWindow.originalImage.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
                st.ScaleX = scale;
                st.ScaleY = scale;

                // 调整Image控件的图素大小
                imageClipWindow.originalImage.RenderTransform = tgnew;
                // 调整Image控件的大小
                //imageClipWindow.originalImage.Width =  bi.Width * scale * 96 / 72;
                //imageClipWindow.originalImage.Height = bi.Height * scale * 96 / 72;
            }
        }

        // 图片裁剪
        private void CutImageCommand()
        {
            Point locationFromScreen = imageClipWindow.clipZone.PointToScreen(new Point(0, 0)); // 矩形区域左上角点转成屏幕坐标
            int x = (int)locationFromScreen.X + 1; // 去掉红色虚线边框
            int y = (int)locationFromScreen.Y + 1;

            // 截取区域内屏幕内容     
            var zoneBmp = new System.Drawing.Bitmap(
                (int)imageClipWindow.clipZone.Width - 2, // 去掉红色虚线边框
                (int)imageClipWindow.clipZone.Height - 2,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            var bmpGraphics = System.Drawing.Graphics.FromImage(zoneBmp);
            bmpGraphics.CopyFromScreen(x, y, 0, 0, zoneBmp.Size); // 区域截屏
            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string fileName = desktop + "\\test.bmp";
            zoneBmp.Save(fileName);
            MessageBox.Show("裁剪图片成功！已保存至：\n" + fileName);

            this.zoneBitmap = zoneBmp; // 如果之后的其他操作需要使用裁剪后的图片，从这里取得。
            imageClipWindow.Hide();
        }

    }
}
